// 云函数入口文件
const cloud = require('xixhaha-7gm9iqp52829762b')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database()
//const callFunction = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  //let {result} = cloud.callFunction()
  const {OPENID} = cloud.getWXContext()
  const {nickName,avatarUrl} =event
  const user_clct = db.collection('user')
  const{data} = await user_clct.where({
    _openid:OPENID
  }).get()
  if(data.length == 0){
    let user = await user_clct.add({
      data:{
        nickName,
        avatarUrl,
        _openid:OPENID
      }
    })
  
  return {
    nickName,
    avatarUrl,
    _openid:OPENID,
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
  }else{
    return data[0]  //如果用户存在,返回该用户信息
  }
}
